package com.yy.job.jobhandler;

import com.xxl.job.core.context.XxlJobHelper;
import com.xxl.job.core.handler.annotation.XxlJob;
import com.yy.device.api.TaskApi;
import com.yy.device.boot.DeviceDataContainer;
import com.yy.device.enums.DeviceDataEnum;
import io.netty.channel.ChannelHandlerContext;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.Map;
import java.util.Objects;

/**
 * 硝化液回流电磁阀
 *
 * @author zhiqiang.sun
 * @date 2022/06/30
 */
@Component
@Slf4j
public class _1_DigestiveFluidJob {

    @Resource
    private TaskApi taskApi;

    /**
     * 硝化液回流电磁阀 开启 job
     * 需要关闭 曝气装置电磁阀
     * 0 0 7,10,13,16 * * ? *
     */
    @XxlJob("digestiveFluidStartHandler")
    public void digestiveFluidStartHandler() throws Exception {
        Integer excludeDeviceCode = -1;
        String params = XxlJobHelper.getJobParam();
        if (!Objects.isNull(params)) {
            excludeDeviceCode = Integer.parseInt(params);
        }
        log.info("digestiveFluidHandler run,params:{}", params);
        Map<Integer, ChannelHandlerContext> contextMap = DeviceDataContainer.CONTEXT_MAP;
        for (Map.Entry<Integer, ChannelHandlerContext> contextEntry : contextMap.entrySet()) {
            Integer deviceCode = contextEntry.getKey();
            if (!Objects.equals(deviceCode, excludeDeviceCode)) {
                taskApi.controDigestiveFluid(deviceCode, DeviceDataEnum.OPEN);
                taskApi.controAeration(deviceCode, DeviceDataEnum.CLOSED);
            }
        }
        log.info("digestiveFluidHandler run,excute number:{}", contextMap.size());
    }

}
